home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Interactive Reference Guide / C-C++ Interactive Reference Guide.iso / c_ref / csource3 / 158_01 / qe6 < prev    next >
Encoding:
Text File  |  1987-10-12  |  9.6 KB  |  459 lines

  1. /*  VERSION 0033  (DATE: 02/11/86)  (TIME: 15:28)  */
  2. /*
  3.     e (qe) screen editor
  4.  
  5.     (C) G. Nigel Gilbert, MICROLOGY, 1981
  6.  
  7.     August-December 1981
  8.     Modified: To QE from e (ver 4.6a) by J.W. Haefner -- Oct 84-Mar 85
  9.  
  10.     FILE: qe6
  11.  
  12.     FUNCTIONS: blockpos,blockops,listfile,reform
  13.  
  14.     PURPOSE: performs block commands
  15.  
  16. */
  17.  
  18. #include "qe.h"
  19. char *firstwhite();        /*returns address of first nonword char*/
  20.  
  21. blockpos(oldpos)
  22. int oldpos;
  23. {
  24.     char c;
  25.     int to;
  26.  
  27.     if (oldpos) puts("| or |P|rev.");
  28.     do {
  29.         resetcursor();
  30.         switch ((c=getkey())) {
  31.         case DOWNKEY    : 
  32.             moveline(1); 
  33.             break;
  34.         case UPKEY    : 
  35.             moveline(-1); 
  36.             break;
  37.         case LEFTKEY    : 
  38.             movechar(-1); 
  39.             break;
  40.         case RIGHTKEY    : 
  41.             movechar(1); 
  42.             break;
  43.         case LEFTWKEY    : 
  44.             moveword(-1); 
  45.             break;
  46.         case RIGHTWKEY    : 
  47.             moveword(1);  
  48.             break;
  49.         case BOLKEY    : 
  50.             sync(0);
  51.             break;
  52.         case EOLKEY    : 
  53.             sync(strlen(text)); 
  54.             break;
  55.         case UPPAGE    : 
  56.             movepage(-1); 
  57.             break;
  58.         case DOWNPAGE    : 
  59.             movepage(0); 
  60.             break;
  61.         case HOMEKEY    : 
  62.             if (jumpline(lastl-cline)) sync(strlen(text));
  63.             break;
  64.         case BOFKEY    : 
  65.             if (jumpline(1-cline)) sync(0);
  66.             break;
  67.         case JUMPKEY    :
  68.             if (expert) xprtmess("J ? ");
  69.             else putmess("Jump to? ");
  70.             scans(ans,6);
  71.             if ((to=atoi(ans)))
  72.                 jumpline(to-cline);
  73.             break;
  74.         case QWIKKEY    :
  75.             info();
  76.             break;
  77.         case REPKEY    : 
  78.             repeat=YES; 
  79.             dofindrep(1); 
  80.             repeat=NO;
  81.             break;
  82.         case 'p'    :
  83.         case 'P'    : 
  84.             if (oldpos) return PREV;
  85.             break;
  86.         case ESCKEY    :
  87.             return FAIL;
  88.         default        : 
  89.             ;
  90.             }
  91.         } 
  92.     while (c != CR);
  93.     return cline;
  94. }
  95.  
  96. blockops()
  97. {
  98.     int oldcline, oldcharn, oldto, oldfrom, op;
  99.     int l, ll, line, *t, shifts, shiftx, cp;
  100.     char  *txt, c, shift[LLIM];
  101.  
  102.     puttext();
  103.     oldcline=cline; 
  104.     oldcharn=charn; 
  105.     oldfrom=from;
  106.     oldto=to;
  107.     if (expert) xprtmess("|Q|/|X|/|R|/|B|/|J-rep| ? ");
  108.     else
  109.       putmess("Q|uit, |X|it w/ save, |R|ead file, |B|lock moves, |J|ump repeat ? ");
  110.     while ((op = getlow()) != 'q' && op != 'x' && op != 'r' && op != 'b' &&
  111.          op != 'j' && op != ESCKEY);
  112.     if (op == ESCKEY) goto clrblk;
  113.     putch(op);
  114.     switch (op)  {
  115.     case 'q':
  116.         if (finish('q')) return (YES);
  117.         return (NO);
  118.         break;
  119.     case 'x':
  120.         if (finish('x')) return (YES);
  121.         return (NO);
  122.         break;
  123.     case 'r':
  124.         return ('r');
  125.         break;
  126.     case 'j':
  127.         if (jmpto > 1) jumpline(jmpto-cline);
  128.         else calcjmp();
  129.         return (NO);
  130.         break;
  131.     default:
  132. #if WWRAP
  133.     if (expert) xprtmess("|W|rt/|P|rint/|S|hft/|R|efrm/|M|v/|C|opy/|D|el ? ");
  134.     else
  135.      putmess("W|rite to file, |P|rint, |S|hift, |R|eformat, |M|ove, |C|opy, or |D|elete block ? ");
  136.     while ((op=getlow()) != 'w' && op != 'p' && op != 's' && op != 'r' 
  137.             && op != 'm' && op != 'c' && op != 'd' && op != ESCKEY);
  138. #else   /*no word wrap*/
  139.     if (expert) xprtmess("|W|rt/|P|rint/|S|hft/|M|v/|C|opy/|D|el ? ");
  140.     else
  141.      putmess("W|rite to file, |P|rint, |S|hift, |M|ove, |C|opy, or |D|elete block ? ");
  142.     while ((op=getlow()) != 'w' && op != 'p' && op != 's'
  143.             && op != 'm' && op != 'c' && op != 'd' && op != ESCKEY);
  144. #endif /*wrap*/
  145.     if (op == ESCKEY) return;
  146.     switch (op) {
  147.     case 'w':
  148.         if (expert) xprtmess("W");
  149.         else puts("Write");
  150.         break;
  151.     case 'p':
  152.         if (expert) xprtmess("P");
  153.         else puts("Print");
  154.         break;
  155.     case 's':
  156.         if (expert) xprtmess("S");
  157.         else puts("Shift");
  158.         break;
  159. #if WWRAP
  160.     case 'r':
  161.         if (expert) xprtmess("R");
  162.         else puts("Reformat");
  163.         break;
  164. #endif
  165.     case 'm':
  166.         if (expert) xprtmess("M");
  167.         else puts("Move");
  168.         break;
  169.     case 'c':
  170.         if (expert) xprtmess("C");
  171.         else puts("Copy");
  172.         break;
  173.     case 'd':
  174.         if (expert) xprtmess("D");
  175.         else puts("Delete");
  176.         break;
  177.         }
  178.     from=cline;
  179.     to=0;
  180.     blocking=YES;
  181.     if (expert) xprtmess("Cursor on end line + <CR>");
  182.     else putmess("|Put cursor on line |end|ing block and press [return]");
  183.     if ( (to=blockpos(oldto)) == FAIL) {
  184.         to=cline;
  185.         goto abort;
  186.         }
  187.     if (to == PREV) {
  188.         moveline(oldfrom-cline);
  189.         from=cline;
  190.         moveline(oldto-cline);
  191.         to=cline;
  192.         }
  193.     if (to < from) {
  194.         l=to; 
  195.         to=from; 
  196.         from=l;
  197.         }
  198.     switch (op) {
  199.     case 'w':
  200.         if (expert) xprtmess("File? ");
  201.         else putmess("File to write to? ");
  202.         if (scans(name,15) != ESCKEY)
  203.             if (exists(name)) writefile(from,to,name,name,NO);
  204.         break;
  205.     case 'p':
  206.         listfile(from,to);
  207.         break;
  208.     case 's':
  209.         if (expert) xprtmess("Changes: ");
  210.         else
  211.          putmess("Delete/insert spaces/tabs| to shift line, and press [return]");
  212.         moveline(from-cline);
  213.         sync(0);
  214.         resetcursor();
  215.         shifts=0;
  216.         while ( (c=getkey()) != CR) {
  217.             switch (c) {
  218.             case DELRIGHT:     
  219.                 if (text[0] == ' ' || text[0] == '\t')
  220.                     deletechar(0);
  221.                 break;
  222.             case ' ':
  223.                 insertchar(' ');
  224.                 break;
  225.             case TAB:
  226.                 insertchar('\t');
  227.                 break;
  228.             case ESCKEY:
  229.                 goto abort;
  230.                 }
  231.             shift[shifts++]=c;
  232.             sync(0);
  233.             resetcursor();
  234.             }
  235.         puttext();
  236.         for (l=from+1; l <= to; l++) {
  237.             gettext(l);
  238.             for (shiftx=0; shiftx < shifts; shiftx++) {
  239.                 switch((c=shift[shiftx])) {
  240.                 case DELRIGHT:     
  241.                     if (*(txt=&text[0]) == ' ' || *txt == '\t')
  242.                         while ( (*txt=*(txt+1))) txt++;
  243.                     break;
  244.                 case ' ':
  245.                 case TAB:
  246.                     if ((cp=strlen(text)) < (LLIM-1)) {
  247.                         for (; cp >= 0; cp--)
  248.                             text[cp+1]=text[cp];
  249.                         text[0]= (c == ' ' ? ' ':'\t');
  250.                         }
  251.                     break;
  252.                     }
  253.                 }
  254.             altered=YES;
  255.             puttext();
  256.             }
  257.         break;
  258. #if WWRAP
  259.     case 'r':
  260.         reform(from,to);
  261.         changed=YES;
  262.         /*charn=adjustc(oldcharn);*/
  263.         from=to=0;
  264.         break;
  265. #endif
  266.     case 'd':
  267.         deltp(from,to-from+1);
  268.         gettext(loc(from,-1));
  269.         charn=adjustc(oldcharn);
  270.         from=to=0;
  271.         changed=YES;
  272.         break;
  273.     case 'm':
  274.     case 'c':
  275.         if (expert) xprtmess("Cursor on line above block + <CR>");
  276.         else
  277.          putmess("|Put cursor on |line under which  block is to go |and press [return]");
  278.         if ( (cline=line=blockpos(0)) == FAIL) {
  279.             cline=oldcline; 
  280.             break;
  281.             }
  282.         for (l=from; l <= to; l++) {
  283.             if ((line=inject(line,getline(l))) == FAIL) break;
  284.             if (op == 'm') {
  285.                 deltp( (l<line?l:l+1),1);
  286.                 if (to < line) {
  287.                     to--; 
  288.                     l--; 
  289.                     line--; 
  290.                     cline--;
  291.                     }
  292.                 }
  293.             else {
  294.                 if (to >= line) to++;
  295.                 if (l >= line) l++;
  296.                 if (l == cline) l=line;
  297.                 }
  298.             }
  299.         from=cline+1; 
  300.         to=line; 
  301.         break;
  302.         }
  303. abort:
  304.     blocking=NO;
  305.     switch (op) {
  306.     case 'w':
  307.     case 'p':
  308.     case 's':
  309.         gettext(oldcline);
  310.         charn=oldcharn;
  311.         putpart(from,to);
  312.         break;
  313.     case 'd':
  314.         putpart(cline,cline+SHEIGHT);
  315.         break;
  316. #if WWRAP
  317.     case 'r':
  318. #endif
  319.     case 'm':
  320.     case 'c':
  321.         putpage();
  322.         break;
  323.         }
  324. clrblk:
  325.     if (expert) xmessoff();
  326.     return NO;
  327.     }   /* outer case */
  328. }      /* func blockops */
  329.  
  330. putpart(start,fin)
  331. int start,fin;
  332. {
  333.     int l, y;
  334.  
  335.     if (start < pfirst) putpage();
  336.     else
  337.     for (l=start, y=topline+(start-pfirst); l <= fin && y <= SHEIGHT;
  338.             l++, y++) {
  339.             if (l == cline) cursory=y;
  340.         putline(l,y);
  341.         }
  342. }
  343.  
  344. listfile(from,to)
  345. int from,to;
  346. {    /*10*/
  347.     int l,cp,i;
  348.     char *t;
  349.  
  350.     puttext();
  351.     for (l=from; l<=to; l++) {    /*20*/
  352.         if (l%10 == 0) putlineno(l);
  353.         for (cp=0, t=getline(l); *t; t++)
  354.             if (*t == '\t') for (i=tabwidth-cp%tabwidth; i>0 ; cp++, i--)
  355.                 bdos(LSTOUT,' ');
  356.             else {    /*30*/
  357.                 /*if (*t > CTRL) bdos(LSTOUT,*t);*/
  358. #if PRINTALL
  359.                 bdos(LSTOUT,*t);
  360. #else
  361.                 if (*t > 0x19) bdos(LSTOUT,*t);        /* send some ctrl char*/
  362.                 else {    /*40*/                        /* for OKI & CITOH */
  363.                     switch (*t) {    /*50*/
  364.                     case (0x08):
  365.                     case (0x0a):
  366.                     case (0x0c):
  367.                     case (0x0d):
  368.                     case (0x18):
  369.                         bdos(LSTOUT,*t);
  370.                         break;
  371.                     default:  {        /*60*/
  372.                         bdos(LSTOUT,'^');
  373.                         bdos(LSTOUT,*t+64);
  374.                         }    /*60*/
  375.                     }    /*50*/
  376.                     }    /*40*/
  377. #endif
  378.                 cp++;
  379.                 }    /*30*/
  380.         bdos(LSTOUT,'\r'); 
  381.         bdos(LSTOUT,'\n');
  382.         if (testkey() == ESCKEY) {    /*70*/
  383.             error("Listing aborted");
  384.             return;
  385.             }    /*70*/
  386.         }    /*20*/
  387. }    /*10*/
  388.  
  389. #if WWRAP
  390. reform(from,to)            /*reformat lines to right margin*/
  391. int from;
  392. int to;
  393. {
  394.     int curline;            /*current line to reform*/
  395.     int inline;                /*current line to inject new line after*/
  396.     int cpos;                /*"current" screen posititon*/
  397.     char rbuf[LLIM],*rbufp;    /*buffer to hold reformed line*/
  398.     char *txtp;                /*pointer to global text*/
  399.     char *posp;                /*position in reformed line*/
  400.     char *diffn;            /*difference of 2 pointers*/
  401.     char achar;
  402.     char toobig,empty;        /*rtmarg exceeded, blnk line tobe reformed*/
  403.     int addblnk;            /*correction if blanks added to line end*/
  404.     
  405.     inline=to-1;
  406.     rbuf[0]='\0';
  407.     rbufp=rbuf;
  408.     txtp=text;
  409.     toobig=empty=0;
  410.     cpos=1;
  411.     for (curline=from;((curline<to) && (curline<=lastl));curline++)  {
  412.         gettext(curline);
  413.         txtp=text;
  414.         addblnk=0;
  415.         do {
  416.             if (achar=*txtp++) {
  417.                 *rbufp++=achar;
  418.                 if (achar=='\t') cpos=cpos+tabwidth-1-(cpos%tabwidth);
  419.                 else cpos++;
  420.                   /* add blnk to line end if none there */
  421.                 addblnk=0;
  422.                 if ( ((achar!=' ') && (achar!='\t')) && !(*(txtp)) ) {
  423.                         *rbufp++ = ' ';
  424.                          cpos++;
  425.                         addblnk=1;
  426.                  }
  427.             }
  428.                 if ((toobig=((cpos > (rtmarg)) &&
  429.                             ( ((*(rbufp-1)!=' ') && (*(rbufp-1)!='\t')))
  430.                               || (!(*(txtp))) )  )
  431.                   || ((empty=(text[0]=='\0')))
  432.                   || ((curline==(to-1)) && ((!*txtp))) ) {
  433.                 if(toobig) {
  434.                     if ((posp=firstwhite(rbuf,cpos,(rbufp))) == rbuf) posp=rbufp;
  435.                     diffn=rbufp-posp;
  436.                     rbufp-=(diffn);
  437.                     txtp-=(diffn-addblnk);
  438.                     empty=NO;
  439.                 }
  440.                 cpos=1;
  441.                 *rbufp = '\0';
  442.                 inject(inline++,rbuf);        /*add new reformatted lines*/
  443.                 if (empty && *rbuf) inject(inline++,text);  /*add empty line*/
  444.                 rbuf[0]='\0';
  445.                 rbufp=rbuf;
  446.             }
  447.         } while (*(txtp) && !empty);
  448.     }
  449.         /*delete old unformatted lines*/
  450.     deltp(from,((to-from)));
  451.     blocking=NO;
  452.     gettext(loc(from,(inline-to+1)));
  453.     sync(0);
  454.     resetcursor();
  455. }
  456. #endif  /*word wrap*/
  457.  
  458. OUT,'\n');
  459.         if (testkey()